import { Component, Node, _decorator,Event, EditBox } from "cc"
import BaseDialog from "./BaseDialog"
import StorageHelper from "../../helper/StorageHelper"
import PoolHelper from "../../helper/PoolHelper"
import Radio from "../common/Radio"
import APIHelper from "../../api/APIHelper"
import CommonEvent from "../../events/CommonEvent"
const {ccclass,property} = _decorator
@ccclass
export default class DebugDialog extends BaseDialog {
    @property(Component)
    private itemContent:Component = null
    @property(EditBox)
    private editBox:EditBox = null
    private _url:string = ''
    protected onLoad(): void {
        //this.editBoxComponent.node.active=false
        const storage=StorageHelper.getInstance()
        const config=storage.api_config
        const pool=PoolHelper.getInstance()
        const arr=config.debug_links
        if(arr[arr.length-1]!='自定义')
        arr.push('自定义')
        let i=0,item:Node;
        const len=arr.length,childrens=this.itemContent.node.children
        for(;i<len;i++){
            if(i<childrens.length){
                item=childrens[i]
            }else{
                pool.getNode('Radio').then(node=>{
                    item=node
                    item.parent=this.itemContent.node
                })
                
            }
            //item.setChildLabel(arr[i],1)
            const radio=item.getComponent(Radio)
            radio.value=arr[i]
            if(storage.api_config.index==i){
                radio.selected=true
                this._url=arr[i]
                APIHelper.getInstance().setURL(this._url)
            }else{
                radio.selected=false
            }
        }
        if(i<childrens.length){
            for(;i<childrens.length;i++){
                item=childrens[i]
                item.parent=null
                pool.putNode('Radio',item)
            }
        }
        this.editBox.string=config.account||'13423676525'
    }
    protected onEnable(): void {
        this.node.on(CommonEvent.RADIO_ITEM_SELECT,this.onRadioItemSelect,this)
    }
    protected onDisable(): void {
        this.node.off(CommonEvent.RADIO_ITEM_SELECT,this.onRadioItemSelect,this)
    }
    private onRadioItemSelect(e:CommonEvent):void{
        console.log(e.data)
        const node=e.target as Node
        const childrens=this.itemContent.node.children
        childrens.forEach((item)=>{
            const radio=item.getComponent(Radio)
            radio.selected=false
        })
        const radio=node.getComponent(Radio)
        radio.selected=true
        const str=e.data.value
        if(str=='自定义'){
            radio.enableEdit=true
        }else{
            radio.enableEdit=false
        }
        this._url=radio.value
        StorageHelper.getInstance().api_config.index=node.getSiblingIndex()
    }
    private onEditBoxChange(value:string):void{
        //console.log(value)
        this._url=value
    }
    private onSure():void{
        const config=StorageHelper.getInstance().api_config
        const childrens=this.itemContent.node.children
        const node=childrens[config.index]
        const radio=node.getComponent(Radio)
        this._url=radio.value
        if(this._url.charAt(this._url.length-1)!='/'){
            alert('以/结尾')
        }else if(this._url.indexOf('http://')==-1&&this._url.indexOf('https://')==-1){
            alert('以http://或https://开头')
        }else if(this._url.charAt(this._url.length-1)=='/'&&(this._url.indexOf('http://')!=-1||this._url.indexOf('https://')!=-1)){
            this.onClose()
            APIHelper.getInstance().setURL(this._url)
            //const config=StorageHelper.getInstance().api_config
            config.debug_links[config.index]=this._url
            config.account=this.editBox.string
            StorageHelper.getInstance().save()
        }else{
            alert('格式不正确！')
        }
    }
}